package com.xcw.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 流程定义管理Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends ActionSupport {
    @Resource
    private ProcessEngine processEngine;
    
    // 查询最新版本的流程定义列表数据
    public String list(){
    	// 流程定义查询对象----查询流程定义表act_re_procdef
    	ProcessDefinitionQuery query = processEngine.getRepositoryService()
    			.createProcessDefinitionQuery();
    	// 添加过滤条件----最新版本
    	query.latestVersion();
    	List<ProcessDefinition> list = query.list();
    	ActionContext.getContext().getValueStack().set("list", list);
    	return "list";
    }
    
    private File deploy;//上传的临时文件
    
    public void setDeploy(File deploy){
    	this.deploy = deploy;
    	
    }
    
    //部署流程定义
    public String deploy() throws FileNotFoundException{
    	DeploymentBuilder deploymentBuilder = processEngine
    			.getRepositoryService().createDeployment();
    	ZipInputStream zip = new ZipInputStream(new FileInputStream(deploy));
    	deploymentBuilder.addZipInputStream(zip);
    	deploymentBuilder.deploy();//部署方法
    	return "toList";
		
    }
    
    public void setId(String id){
    	this.id = id;
    }
    
    private String id;//接收参数---流程定义id
    
    //查看png图片
    public String viewpng(){
    	// 使用框架提供的api获取png图片的输入流
    	InputStream pngStream = processEngine.getRepositoryService().getProcessDiagram(id);
    	ActionContext.getContext().put("pngStream", pngStream);
    	return "viewpng";
    }
}
